<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BApplication
 *
 * @author jianfeng
 */
class BApplication extends BNode {

    const Personal_Home_Page = 'personalHome';

    //put your code here

    private $currentUser = null;
    private $db = null;
    private $appUi = null;
    private $configManager = null;
    private $appPool = null;
    private $activeService = null;
    private $activeApp = null;
    private $activeOpp = null;
    private $currentNode = null;
    //private $transitApp = null;
    private $transitObj = null;
    private $count = 0;
    private $isSetDefaultTimeZone = false;
    public $basePath = null;
    protected $authPath = null;
    protected $dataPath = null;
    protected $dbPath = null;
    public $configPath = null;
    public $uiPath = null;
    public $appPath = null;

    public function __construct() {
        //$this->initImport();
        //$config = new BApplicationConfig($this);
        $config = $this->getConfigManager();
        $config->init();
        $this->setDefaultTimeZone();
    }

    /**
     *
     * @return BUser
     */
    public function getCurrentUser() {
        if ($this->currentUser == null)
            $this->currentUser = new BUser();

        return $this->currentUser;
    }

    public function getCurrentNode() {
        if ($this->currentNode == NULL)
            return $this;
        return $this->currentNode;
    }

    public function setCurrentNode($node = NULL) {

        if ($node == NULL) {
            $this->currentNode = $this;

            if (Blyn::app()->getActiveService() != NULL)
                $this->currentNode = Blyn::app()->getActiveService();
            if (Blyn::app()->getActiveServiceApp() != NULL)
                $this->currentNode = Blyn::app()->getActiveServiceApp();
            if (Blyn::app()->getActiveOperation() != NULL)
                $this->currentNode = Blyn::app()->getActiveOperation();
        } else {
            $this->currentNode = $node;
        }
        $uiNode = $this->currentNode->getUIManager();
        $uiNode->resetTransitVariable();
        //$uiNode->setCurrentScreenFlow();
        //$uiNode->setCurrentScreen();       
    }

    public function setContext($data = NULL) {

        if ($data == NULL) {
            $deConfig = $this->getUIManager()->deUrl();

            $operationId = isset($deConfig['OperationId']) ? $deConfig['OperationId'] : FALSE;
            $serviceAppId = isset($deConfig['AppId']) ? $deConfig['AppId'] : FALSE;
            $serviceId = isset($deConfig['ServiceId']) ? $deConfig['ServiceId'] : FALSE;
        } else {
            $serviceId = isset($data['serviceId']) ? $data['serviceId'] : FALSE;
            $serviceAppId = isset($data['serviceAppId']) ? $data['serviceAppId'] : FALSE;
            $operationId = isset($data['operationId']) ? $data['operationId'] : FALSE;
        }

        if (is_numeric($serviceId) && $serviceId > 0)
            Blyn::app()->setActiveService(new BService($serviceId));

        if (!is_numeric($serviceAppId)) {
            $activService = Blyn::app()->getActiveService();
            if ($activService instanceof BService)
                Blyn::app()->setActiveServiceApp($activService->getServiceEngine());
        } else {
            Blyn::app()->setActiveServiceApp(BServiceApp::getInstanceById($serviceAppId, Blyn::app()->getActiveService()));
        }

        if (is_numeric($operationId)) {
            $operation = new BOperation($operationId, Blyn::app()->getActiveService());
            Blyn::app()->setActiveServiceApp($operation->getApp());
            Blyn::app()->setActiveOperation($operation);
        } else {
            Blyn::app()->setActiveOperation(NULL);
        }

        if (isset($data['renderNode']) && $data['renderNode'] == BUIApp::HomeNode_PersonalService) {
            $currentUser = Blyn::app()->getCurrentUser();
            $personalService = Blyn::app()->setActiveService($currentUser->getPersonalService());
            return $this->setCurrentNode($personalService);
        }

        $this->setCurrentNode();
    }

    /**
     *
     * @return BAppDb
     */
    public function getAppDb() {
        if ($this->db == null)
            $this->db = new BAppDb ();

        return $this->db;
    }

    /**
     *
     * @return BUIApp
     */
    public function getAppUI() {
        if ($this->appUi == null) {
            $this->appUi = new BUIApp($this);
        }

        return $this->appUi;
    }

    public function getUIManager($style = NULL) {
        return $this->getAppUI();
    }

    /**
     *
     * @return BApplicationConfig 
     */
    public function getConfigManager() {
        if ($this->configManager == NULL)
            $this->configManager = new BApplicationConfig($this);
        return $this->configManager;
    }

    /**
     *
     * @return BAppPool 
     */
    public function getAppPool() {
        if ($this->appPool == NULL)
            $this->appPool = new BAppPool ();

        return $this->appPool;
    }

    /**
     *
     * @return BService
     */
    public function getActiveService() {
        return $this->activeService;
    }

    /**
     *
     * @return BServiceApp
     */
    public function getActiveServiceApp() {
        /*if ($this->activeApp == NULL)
            $this->activeApp = new BServiceEngine;
         * 
         */
        return $this->activeApp;
    }

    /**
     *
     * @return BOperation
     */
    public function getActiveOperation() {
        /*
          if ($this->activeOpp == NULL && $this->getCurrentNode() instanceof BOperation)
          $this->activeOpp = $this->getCurrentNode();
         * 
         */
        return $this->activeOpp;
    }

    public function setActiveService($service) {
        $this->activeService = $service;
    }

    public function setActiveServiceApp($app) {
        $this->activeApp = $app;
    }

    public function setActiveOperation($operation) {
        if ($operation == NULL)
            $this->activeOpp = FALSE;
        else {
            $this->activeOpp = $operation;
        }
    }

    public function addTransitVariable($name, $value) {
        //$user = $this->getCurrentUser();
        //$user->addTransitVariable($name, $value);

        $tObj = $this->getTransitObj();

        $tObj->addTransitVariable(BTransitObj::Scope_Application, $name, $value);
    }

    public function getTransitVariable($name) {
        //$user = $this->getCurrentUser();
        //return $user->getTransitVariable($name);
        $tObj = $this->getTransitObj();
        $tObj->getTransitVariable(BTransitObj::Scope_Application, $name);
    }

    /**
     * 
     * @return BTransitObj
     */
    public function getTransitObj() {
        if ($this->transitObj == NULL)
            $this->transitObj = new BTransitObj ();

        return $this->transitObj;
    }

    public function setDefaultTimeZone($timeZoneString = NULL) {
        $defaultTimeZone = 'Asia/Singapore';
        if ($timeZoneString != NULL)
            $defaultTimeZone = $timeZoneString;
        date_default_timezone_set($defaultTimeZone);
    }

    public function getDefaultTimeZone() {
        if (!$this->isSetDefaultTimeZone)
            $this->setDefaultTimeZone();

        return date_default_timezone_get();
    }

    public function getUnicodeId() {
        return $this->count++;
    }

}

?>
